package cn.zjj.basic.controller;

import cn.zjj.basic.dto.SmsCodeDto;
import cn.zjj.basic.exception.BusinessException;
import cn.zjj.basic.service.IVerifyCodeService;
import cn.zjj.basic.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 验证码接口
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    //图片验证码
    @GetMapping("/image/{code}")
    public JsonResult getImgCode(@PathVariable("code") String code){
        try {
           String baseStr = verifyCodeService.getImgCode(code);
           return JsonResult.me().setResultObj(baseStr);
        }
        catch (BusinessException e) {
            e.printStackTrace();
            return new JsonResult().runtimeException(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"服务器繁忙!");
        }
    }
    //手机验证码
    @PostMapping("/smsCode")
    public JsonResult getSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeService.sendSmsCode(smsCodeDto);
            return JsonResult.me();
        }
        catch (BusinessException e) {
            e.printStackTrace();
            return new JsonResult().runtimeException("手机验证码发送失败,"+e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"系统繁忙");
        }
    }
    //binderSmsCode
    @PostMapping("/binderSmsCode")
    public JsonResult binderSmsCode(@RequestBody Map<String,String> map){
        try {
            String phone = map.get("phone");
            verifyCodeService.binderSmsCode(phone);
            return JsonResult.me();
        }
        catch (BusinessException e) {
            e.printStackTrace();
            return new JsonResult().runtimeException("手机验证码发送失败,"+e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"系统繁忙");
        }
    }
}
